pragma Singleton

import QtQuick 2.11
import QtMultimedia 5.9
import Qt.labs.settings 1.0

import cn.leamus.gamedata 1.0

//import "Global"
import LGlobal 1.0
//import "_Global/Global.js" as GlobalJS


//包含 框架系统的一些配置、函数、变量等
QtObject {

    id: root
    objectName: "Global"



    property QtObject window: null

    property Settings settings: GameSettings {
        category: "Config"
        //property alias previousVersion: settings.previousVersion
    }

    //设置
    property QtObject config: GameConfig {
        //分辨率
        property int resolution: {
            switch(Qt.platform.os) {
                case "android":
                    return 2;
                default:
                    return 1;
            }
        }
    }

    //全局共享资源
    property QtObject resource: GameResource {
    }

    //游戏数据
    property QtObject gameData: GameData {

        //游戏共享数据
        property var shareGameData: {"type": "shareGameData"
            //, "arrayMyPets": []
            //, "arrayFriendPets": []
            //, "arrayEnemyPets": []
        }

        property int nGameFrameIndex: 0         //帧序号(与服务器同步,用来判断帧是否是此帧的)
        property int nGameFrameSendCount: 0     //一帧中已发送的帧数(断线重连后会从服务器赋值)

        //property var mapPlayerChangeSocketIDCache: ({})

    }

    //坐标系统
    property QtObject gamePos: GamePos {
        //property point posWindow: Qt.point(1920,1080) //基像素
        property size sizeWindow :
        switch(Qt.platform.os) {
            case "android":
                return Qt.size(800,600); //基像素
            default:
                return Qt.size(800,600); //基像素
        }
    }


    //函数
    property QtObject gameFunctions: GameFunctions {

        //发送 同步 数据
        function sendGameSyncData(syncData) {
            console.debug("[game]sendGameSyncData!");
            GameManager.sl_qml_SendGameSyncData(syncData);
        }
        //发送 帧 数据
        function sendGameFrameData(frameData) {
            console.debug("[game]sendGameFrameData!");
            Global.gameData.nGameFrameSendCount ++;
            GameManager.sl_qml_SendGameFrameData(frameData);
        }
        //发送 所有数据
        function sendGameSyncAllData() {
            console.debug("[game]sendGameSyncAllData!");
            GameManager.sl_qml_SendGameSyncAllData(Global.gameData.shareGameData);
        }


        function random(n1,n2) {
            if(n1 === n2)
                return n1;
            else if(n2 < n1)
            {
                n1=n1+n2;
                n2=n1-n2;
                n1=n1-n2;
            }
            var n = GameCore.socketInfo.getRandomNumber(!GameCore.netPlay);
            //console.debug("debug3:", n, GameCore.netPlay)
            return (n % ( n2 - n1 )) + n1;
            //return n1 + Math.round((n2-n1) * GameCore.socketInfo.getRandomNumber(!GameCore.netPlay));
        }

        function randTarget(n, m) {		//	m分之n
            if(n > m || n < 0)return -1;	//不符合
            //if(GameCore.socketInfo.getRandomNumber(!GameCore.netPlay) < n / m)return 1;	//命中
            if(GameCore.socketInfo.getRandomNumber(!GameCore.netPlay) % m < n)return 1;    //命中
            return 0;
        }

    }

    property QtObject gameSignals: GameSignals {

    }



//自适应系统方法1:
    //property real multiplierX: 1 //default multiplier, but can be changed by user
    //property real multiplierY: 1 //default multiplier, but can be changed by user
    //这两个在game的Component.onCompleted中修改
    property real multiplierW: 1 //default multiplier, but can be changed by user
    property real multiplierH: 1 //default multiplier, but can be changed by user
    //function dpX(numbers) {
    //    return Math.round(numbers * multiplierX);
    //}
    //function dpY(numbers) {
    //    return Math.round(numbers * multiplierY);
    //}
    function dpW(numbers) {
        return Math.round(numbers * multiplierW);
    }
    function dpH(numbers) {
        return Math.round(numbers * multiplierH);
    }
    /*在Window中添加:
    onWidthChanged: {
        multiplierW = root.width / maxSize.width;

    }
    onHeightChanged: {
        multiplierH = root.height / maxSize.height;
    }*/



//自适应系统方法2:
    /*
    //Item中添加:
    transform: [
        Scale {
            origin {

            }
            xScale:
                root.width / maxSize.width
            yScale:
                root.height / maxSize.height
        }

    ]
    */




//其他方法:
    /*
    //界面size
    property real pixelDensity: 4.46
    property real multiplierH: height/600 //default multiplier, but can be changed by user
    property real multiplierW: width/1024 //default multiplier, but can be changed by user

    function dpH(numbers) {

        return Math.round(numbers*((pixelDensity*25.4)/160)*multiplierH);
    }
    function dpW(numbers) {

        return Math.round(numbers*((pixelDensity*25.4)/160)*multiplierW);
    }
*/




    //返回文件系统中的只读
    function _FixLocalPath_R(path) {  //只读文件路径转换
        switch(Qt.platform.os) {
        case "android":
            //console.debug("assets:/" + path)
            return "assets:/" + path;    //也可以是qrc:/ 或 相对绝对路径
        case "windows":
            //console.debug("file:" + path)
            return "file:" + path;       //也可以是qrc:/ 或 相对绝对路径     //也可以是qrc:/ 或 相对绝对路径
        case "osx":     //苹果的比较特殊!!!
            return "file:" + Platform.applicationDirPath() + "/" + path;       //也可以是qrc:/ 或 相对绝对路径
        case "ios":     //苹果的比较特殊!!!
            return "file:" + Platform.applicationDirPath() + "/" + path;
        default:
            //console.debug("qrc:/" + path)
            return "qrc:/" + path;       //也可以是qrc:/ 或 相对绝对路径
        }
        return Qt.resolvedUrl(path);    //转换为绝对路径
    }
    //返回文件系统中的读写
    function _FixLocalPath_W(path) {  //读写文件路径转换
        switch(Qt.platform.os) {
        case "android":
            //console.debug(path)
            //return path;                 //也可以是 相对绝对路径
            return "file:" + path;       //也可以是 相对绝对路径
        case "windows":
            //console.debug("file:" + path)
            return "file:" + path;       //也可以是 相对绝对路径
        case "osx":     //苹果的比较特殊!!!
            return "file:" + Platform.applicationDirPath() + "/" + path;       //也可以是qrc:/ 或 相对绝对路径
        case "ios":     //苹果的比较特殊!!!
            return "file:" + Platform.applicationDirPath() + "/" + path;
        default:
            //console.debug(path)
            return path;                 //也可以是 相对绝对路径
        }
        return Qt.resolvedUrl(path);    //转换为绝对路径
    }

    //返回资源系统（qrc）中的只读
    //鹰：注意：每个QML引用的资源相对路径，都是相对于自个QML，所以不能用 相对路径和resolvedUrl
    function _FixResourcePath_R(path) {  //只读资源路径转换
        switch(Qt.platform.os) {
        case "windows":
            if(Platform.compileType() === "release")
                return "qrc:/" + path;
            //console.debug("file:" + path)
            return "file:" + Platform.applicationDirPath() + "/" + path;    //转换为绝对路径
        case "linux":
            if(Platform.compileType() === "release")
                return "qrc:/" + path;
            //console.debug("file:" + path)
            return "file:" + Platform.applicationDirPath() + "/" + path;    //转换为绝对路径
        case "osx":     //苹果的比较特殊!!!
            if(Platform.compileType() === "release")
                return "qrc:/" + path;
            return "qrc:/" + path;
        case "ios":     //苹果的比较特殊!!!
            return "qrc:/" + path;
        case "android":
            //console.debug("assets:/" + path)
            return "qrc:/" + path;
        default:
            //console.debug("qrc:/" + path)
            return "qrc:/" + path;
        }
    }



    Component.onCompleted: {
        console.debug("Global.qml加载完毕")
    }
}
